package com.ruoyi.project.system.controller;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.project.common.UserManager;
import com.ruoyi.project.plus.mapper.RoomMapper;
import com.ruoyi.project.system.domain.RoomTypes;
import com.ruoyi.project.system.domain.User;
import com.ruoyi.project.system.domain.request.RequestRoom;
import com.ruoyi.project.system.service.IRoomTypesService;
import com.ruoyi.project.system.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.Rooms;
import com.ruoyi.project.system.service.IRoomsService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2020-05-23
 */
@RestController
@RequestMapping("/app/Rooms")
public class RoomsController extends BaseController
{
//    @Autowired
//    private IRoomsService roomsService;
    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private IRoomTypesService roomsTypeService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IUserService userService;

    /**
     * 查询【获取厅列表】列表
     */
    @ApiOperation("获取厅列表")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam(value = "厅类型") String type)
    {
        startPage();
        Rooms rooms = new Rooms();
        if(!"热门".equals(type)){
            rooms.setType(type);
        }
        rooms.setStatus(1);
        List<Rooms> list = roomMapper.selectList(new QueryWrapper<>());// roomsService.selectRoomsList(rooms);
        return getDataTable(list);
    }

    /**
     * 新增【创建厅功能】
     */
    @ApiOperation("创建厅(需审核)")
    @Log(title = "【创建厅功能】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RequestRoom rooms)
    {
        String uid = tokenService.getUidFromToken(ServletUtils.getRequest());
        if(uid.length() < 1) {
            return AjaxResult.error(10002,"未登录，请授权之后再使用");
        }
        User user = UserManager.getManager().getInfoFromUid(uid);
        Rooms newRooms = new Rooms();
        newRooms.setUserid(user.getUid());
        newRooms.setUsername(user.getNick());
        newRooms.setStatus(0);//审核中
        newRooms.setType(rooms.getType());
        newRooms.setThumb(rooms.getThumb());
        newRooms.setTitle(rooms.getTitle());
        newRooms.setCreateTime(new Date().getTime());
        return toAjax(roomMapper.insert(newRooms));
    }
    /**
     * 查询【获取厅列表】列表
     */
    @ApiOperation("获取厅类型列表")
    @GetMapping("/types")
    public TableDataInfo list()
    {
        startPage();
        List<RoomTypes> list = roomsTypeService.selectRoomTypesList(new RoomTypes());
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:Rooms:export')")
//    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
//    @GetMapping("/export")
//    public AjaxResult export(Rooms rooms)
//    {
//        List<Rooms> list = roomsService.selectRoomsList(rooms);
//        ExcelUtil<Rooms> util = new ExcelUtil<Rooms>(Rooms.class);
//        return util.exportExcel(list, "Rooms");
//    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:Rooms:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(roomMapper.selectById(id));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:Rooms:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Rooms rooms)
    {
        return toAjax(roomMapper.update(rooms,new UpdateWrapper<>()));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:Rooms:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(roomMapper.deleteById(ids[0]));
    }
}
